#include "FireBall.h"
#include "Global.h"
#include "CGamePlayStatics.h"

SDL_Surface * cFireBall::FIREBALLS[4] = { NULL };
cFireBall::cFireBall()
{
	online = 0;
}

cFireBall::~cFireBall()
{

}

void cFireBall::InitFireBallImg()
{
	FIREBALLS[0] = LoadIMG(DATADIR "gfx/fire1.png");
	FIREBALLS[1] = LoadIMG(DATADIR "gfx/fire2.png");
	FIREBALLS[2] = LoadIMG(DATADIR "gfx/fire3.png");
	FIREBALLS[3] = LoadIMG(DATADIR "gfx/fire4.png");
}

bool cFireBall::init(int _x, int _y, int dir)
{
	if (online)
		return 0;
	PLAYSOUND1(S_FIREBALL);
	this->x = _x;
	this->y = _y;
	this->online = 1;
	this->x_speed = dir * 3;
	this->y_speed = 3;
	this->ani = 0;
	return 1;
}

void cFireBall::update()
{

}

bool cFireBall::init()
{
	return true;
}

void cFireBall::move(float dt, cPlayer* mario)
{
	if (!online)
		return;
	ani += 0.2;
	if (ani >= 4)
		ani = 0;
	center_touch = LEVEL->posValid(x, y + 2) + LEVEL->posValid(x + 20, y + 2);
	down_touch = LEVEL->posValid(x + 10, y + 20);

	x += x_speed;
	y += y_speed;
	if (down_touch)
		y_speed = -5;
	y_speed += 0.5;

	rect.x = x - cam_x;
	rect.y = y - cam_y;

	if (center_touch || rect.x< -30 || rect.x > WIDTH + 50)
		online = 0;

	SDL_UpperBlit(cFireBall::FIREBALLS[(int)ani], 0, screen, &rect);
}

void cFireBall::itemCollision(cPlayer* mario, std::vector<NpcItem *> &items)
{
	std::vector<NpcItem *>::iterator npcIt = items.begin();
	for (; npcIt != items.end(); npcIt++) {
		NpcItem *item = *npcIt;
		if (collision(&this->rect, &item->GetItemRect()))
		{
			CDamageType damageType;
			CGamePlayStatics::ApplyDamage(item, 10, this, damageType);
			online = 0;
		}
	}
	/*if (BOWSER != NULL) {
		if (collision(&this->rect, &BOWSER->rect))
		{
			BOWSER->hit();
			online = 0;
		}
	}*/
	/*for (int i = 0; i < GOOMBAcount; i++)
		if (!GOOMBA[i]->DEATHcount && !GOOMBA[i]->dying && collision(&rect, &GOOMBA[i]->rect))
		{
			GOOMBA[i]->die();
			online = 0;
		}
	for (int i = 0; i < TURTLEFLYcount; i++)
		if (!TURTLEFLY[i]->dead && !TURTLEFLY[i]->dying && collision(&rect, &TURTLEFLY[i]->rect))
		{
			TURTLEFLY[i]->die();
			online = 0;
		}
	for (int i = 0; i < TURTLEcount; i++)
		if (!TURTLE[i]->dead && !TURTLE[i]->dying && collision(&rect, &TURTLE[i]->rect))
		{
			TURTLE[i]->die();
			online = 0;
		}
	for (int i = 0; i < SPINYcount; i++)
		if (!SPINY[i]->dead && !SPINY[i]->dying && collision(&rect, &SPINY[i]->rect))
		{
			SPINY[i]->die();
			online = 0;
		}*/
}

void cFireBall::collisionWithOthers(NpcItem* sprite)
{

}

float cFireBall::TakeDamage(float DamageAmount, struct CDamageEvent const& DamageEvent, NpcItem* DamageCauser)
{
	return 10;
}

SDL_Rect& cFireBall::GetItemRect()
{
	this->rect.x = x - cam_x;
	this->rect.y = y - cam_y;
	return this->rect;

}